package test06;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

/**
 * 复杂+复杂类型
 * @author Administrator
 *
 */
public class DozerTest06 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		
		/**
		 * PO或VO，双方都是复杂类型的属性，且对应子属性也是类型和名称都不同：必须得配置XML
		 */
		DozerBeanMapper mapper = new DozerBeanMapper();
		
		List mf = new ArrayList();
		mf.add("test06/test06.xml");//格式：“目录/XML文件名”，若无目录时默认为ClassPath目录
		mapper.setMappingFiles(mf);

		//示例一：UserPO -> UserVO
		UserPO po1 = new UserPO();
		InfoPO info1 = new InfoPO();
		info1.setId(100);
		info1.setName("123");
		info1.setAge(10);
		info1.setHeight(170.5F);
		info1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2008-08-08"));
		po1.setInfo(info1);
		
		UserVO vo1 = mapper.map(po1, UserVO.class);

		System.out.println("示例一：由PO向VO转换");
		System.out.println("UserPO	->	UserVO");
		
		System.out.println(po1.getInfo().getId()+"[int]    "+vo1.getInfo2().getId2()+"[Integer]");
		System.out.println(po1.getInfo().getName()+"[String]    "+vo1.getInfo2().getName2()+"[int]");
		System.out.println(po1.getInfo().getAge()+"[Integer]    "+vo1.getInfo2().getAge2()+"[String]");
		System.out.println(po1.getInfo().getHeight()+"[float]    "+vo1.getInfo2().getHeight2()+"[String]");
		System.out.println(po1.getInfo().getBirthday()+"[Date]    "+vo1.getInfo2().getBirthday2()+"[String]");
		
		//*
		//示例二：UserVO -> UserPO
		UserVO vo2 = new UserVO();
		InfoVO info2 = new InfoVO();
		info2.setId2(null);
		info2.setName2(123);
		info2.setAge2("20");
		info2.setHeight2("170.5F");
		info2.setBirthday2("2010-10-10");//String -> Date：需要配置XML
		vo2.setInfo2(info2);
		
		UserPO po2 = new UserPO();
		mapper.map(vo2, po2);
		//UserPO po2 = mapper.map(vo2, UserPO.class);

		System.out.println("\n示例二：由VO向PO转换");
		System.out.println("UserVO	->	UserPO");
		System.out.println(vo2.getInfo2().getId2()+"[Integer]    "+po2.getInfo().getId()+"[int]");
		System.out.println(vo2.getInfo2().getName2()+"[int]    "+po2.getInfo().getName()+"[String]");
		System.out.println(vo2.getInfo2().getAge2()+"[String]    "+po2.getInfo().getAge()+"[Integer] ");
		System.out.println(vo2.getInfo2().getHeight2()+"[String]    "+po2.getInfo().getHeight()+"[float]");
		System.out.println(vo2.getInfo2().getBirthday2()+"[String]    "+po2.getInfo().getBirthday()+"[Date]");
		//*/
	}
}
